package com.hzhr.iot.user.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrStringUtils;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.user.Global;
import com.hzhr.iot.user.constant.UserOptions;
import com.hzhr.iot.user.constant.UserOptionsName;
import com.hzhr.iot.user.constant.UserOptionsTypeName;
import com.hzhr.iot.user.constant.User_ErrorsEnum;
import com.hzhr.iot.user.convertor.custom.CompanyCustomConvertor;
import com.hzhr.iot.user.dao.CompanyDao;
import com.hzhr.iot.user.dao.DeptDao;
import com.hzhr.iot.user.dao.UserDao;
import com.hzhr.iot.user.dao.helper.CompanyDaoHelper;
import com.hzhr.iot.user.dao.helper.UserDaoHelper;
import com.hzhr.iot.user.entity.CompanyEntity;
import com.hzhr.iot.user.entity.DeptEntity;
import com.hzhr.iot.user.entity.UserEntity;
import com.hzhr.iot.user.entity.ext.CompanyEntityExt;
import com.hzhr.iot.user.model.company_basicInfo.Company_BasicInfoGetModel;
import com.hzhr.iot.user.model.company_basicInfo.Company_BasicInfoModel;
import com.hzhr.iot.user.model.company_basicInfo.Company_BasicInfoPostModel;
import com.hzhr.iot.user.model.company_basicInfo.Company_BasicInfoPutModel;
import com.hzhr.iot.user.queryModel.custom.CompanyCustomQueryModel;
import com.hzhr.iot.user.queryModel.custom.UserCustomQueryModel;
import com.hzhr.iot.user.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = CompanyEntity.class, model = Company_BasicInfoModel.class)
public class Company_BasicInfoService extends BaseService {
    @Autowired
    CompanyDaoHelper companyDaoHelper;
    @Autowired
    CompanyDao companyDao;
    @Autowired
    CompanyService companyService;
    @Autowired
    CompanyCustomConvertor companyCustomConvertor;
    @Autowired
    UserOptions userOptions;
    @Autowired
    DeptDao deptDao;
    @Autowired
    UserDao userDao;
    @Autowired
    UserDaoHelper userDaoHelper;
    @Autowired
    Company_BasicInfoService company_basicInfoService;

    @Autowired
    Global global;

    //region javadoc注释 : put
    /**
     * 公司创始人更新其所创建的公司的基本信息 <br/><br/>
     * 简介&nbsp;:&nbsp;更新公司信息 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询公司&nbsp;:&nbsp;(√) <br/>
     * &nbsp;查询登录人所创建的公司(COMPANY)company_1，如果company_1不存在，抛出异常201； <br/>
     * 2.&nbsp;更新基本信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body更新company_1的基本信息； <br/>
     * 3.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询更新结果，组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * COMPANY_BASICINFOSERVICE_PUT_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询公司<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 公司不存在<br/>
     * @see Company_BasicInfoPutModel body <br/>
     * @see Company_BasicInfoGetModel result <br/>
     */ 
    //endregion
    //region 说明 : put
    //公司创始人更新其所创建的公司的基本信息
    //简介 : 更新公司信息
    //步骤 :
    //1. 查询公司 : (√)
    // 查询登录人所创建的公司(COMPANY)company_1，如果company_1不存在，抛出异常201；
    //2. 更新基本信息 : (√)
    // 根据body更新company_1的基本信息；
    //3. 返回信息 : (√)
    // 根据条件参数查询更新结果，组装响应并返回；
    //endregion
    //region 参数 : put
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })

    //endregion
    //region 请求 : put
    @RequestMapping(value = "/companys/basic-infos", method = RequestMethod.PUT)
    //endregion
    public Company_BasicInfoGetModel put(Map<String, String> header, Map<String, Object> query,
                                                    Company_BasicInfoPutModel body) throws ServiceException {
        /* 查询公司 */
        Integer loginUserId = getUserId(header);
        CompanyEntity company_1 = companyDaoHelper.getByCreateUserId(loginUserId);
        if (company_1 == null) {
            throw new ServiceException(User_ErrorsEnum.COMPANY_BASICINFOSERVICE_PUT_201);
        }
        int companyId_1 = company_1.getId();

        /* 更新基本信息 */
        String shortName = body.getShortName();
        String addr = body.getAddr();
        String contacts = body.getContacts();
        Long phone = body.getPhone();
        String email = body.getEmail();
        company_1.setShortName(shortName);
        company_1.setAddr(addr);
        company_1.setContacts(contacts);
        company_1.setPhone(HzhrTypeUtil.long2string(phone));
        company_1.setEmail(email);
        company_1.setUpdateUserId(loginUserId);
        company_1.setUpdateTime(new Date());
        int changeCount = companyDao.update(company_1);

        /* 返回信息 */
        Company_BasicInfoGetModel result = company_basicInfoService.get(initHeader(header, AccessModeEnum.GLOBAL), query);
        result.setChangeCount(changeCount);
        return result;
    }

    //region javadoc注释 : post
    /**
     * 用户创建公司 <br/><br/>
     * 简介&nbsp;:&nbsp;创建公司；创建同名顶级部门；更新登录用户信息 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;验证登录用户类型&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据登录人的userId查询用户(USER)user_1，如果user_1.type不是游离用户，抛出异常201； <br/>
     * 2.&nbsp;验证公司名称&nbsp;:&nbsp;(√) <br/>
     * &nbsp;验证是否存在某个公司(COMPANY)的name与body.name相同，如果存在，则抛出异常202； <br/>
     * 3.&nbsp;创建公司&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据body创建公司(COMPANY)company_1，并且同时设置以下属性： <br/>
     * &nbsp;(1)&nbsp;company_1.resourceCode&nbsp;=&nbsp;'0'； <br/>
     * 4.&nbsp;更新公司的资源代码&nbsp;:&nbsp;(√) <br/>
     * &nbsp;设置company_1.resourceCode； <br/>
     * &nbsp;(1)&nbsp;company_1.resourceCode设置规则：将company_1.id左补零至4位字符串str_1,&nbsp;company_1.resourceCode&nbsp;=&nbsp;'01'&nbsp;+&nbsp;str_1； <br/>
     * 5.&nbsp;创建同名顶级部门&nbsp;:&nbsp;(√) <br/>
     * &nbsp;在company_1下创建同名的部门(DEPT)dept_1，并且同时设置以下属性： <br/>
     * &nbsp;(1)&nbsp;dept_1.code&nbsp;=&nbsp;company_1.id&nbsp;+&nbsp;'-01'； <br/>
     * &nbsp;(2)&nbsp;dept_1.level&nbsp;=&nbsp;1； <br/>
     * &nbsp;(3)&nbsp;dept_1.parentId&nbsp;=&nbsp;0； <br/>
     * &nbsp;(3)&nbsp;dept_1.resourceCode&nbsp;=&nbsp;company_1.resourceCode&nbsp;+&nbsp;'01'； <br/>
     * 6.&nbsp;更新登录用户信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;更新user_1的以下属性： <br/>
     * &nbsp;(1)&nbsp;user_1.type设置为公司创始人； <br/>
     * &nbsp;(2)&nbsp;user_1.companyId&nbsp;=&nbsp;company_1.id； <br/>
     * &nbsp;(3)&nbsp;user_1.deptId&nbsp;=&nbsp;dept_1.id； <br/>
     * &nbsp;(4)&nbsp;user_1.resourceCode&nbsp;=&nbsp;company_1.resourceCode； <br/>
     * 7.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数查询创建结果，组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * COMPANY_BASICINFOSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证登录人类型<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 登录人不是游离用户，无法进行操作<br/>
     * COMPANY_BASICINFOSERVICE_POST_202 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 验证公司名称<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 公司名称已存在<br/>
     * @see Company_BasicInfoPostModel body <br/>
     * @see Company_BasicInfoGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //用户创建公司
    //简介 : 创建公司；创建同名顶级部门；更新登录用户信息
    //步骤 :
    //1. 验证登录用户类型 : (√)
    // 根据登录人的userId查询用户(USER)user_1，如果user_1.type不是游离用户，抛出异常201；
    //2. 验证公司名称 : (√)
    // 验证是否存在某个公司(COMPANY)的name与body.name相同，如果存在，则抛出异常202；
    //3. 创建公司 : (√)
    // 根据body创建公司(COMPANY)company_1，并且同时设置以下属性：
    // (1) company_1.resourceCode = '0'；
    //4. 更新公司的资源代码 : (√)
    // 设置company_1.resourceCode；
    // (1) company_1.resourceCode设置规则：将company_1.id左补零至4位字符串str_1, company_1.resourceCode = '01' + str_1；
    //5. 创建同名顶级部门 : (√)
    // 在company_1下创建同名的部门(DEPT)dept_1，并且同时设置以下属性：
    // (1) dept_1.code = company_1.id + '-01'；
    // (2) dept_1.level = 1；
    // (3) dept_1.parentId = 0；
    // (3) dept_1.resourceCode = company_1.resourceCode + '01'；
    //6. 更新登录用户信息 : (√)
    // 更新user_1的以下属性：
    // (1) user_1.type设置为公司创始人；
    // (2) user_1.companyId = company_1.id；
    // (3) user_1.deptId = dept_1.id；
    // (4) user_1.resourceCode = company_1.resourceCode；
    //7. 返回信息 : (√)
    // 根据条件参数查询创建结果，组装响应并返回；
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/companys/basic-infos", method = RequestMethod.POST)
    //endregion
    @Transactional(rollbackFor = Exception.class)
    public Company_BasicInfoGetModel post(Map<String, String> header, Map<String, Object> query,
                                Company_BasicInfoPostModel body) throws ServiceException {
        /* 验证登录用户类型 */
        UserCardModel userCard = getUserCard(header);
        int loginUserId = userCard.getUserId();
        UserEntity user_1 = userDaoHelper.getById(loginUserId);
        String freeCode = userOptions.getCode(UserOptionsTypeName.USER_TYPE, UserOptionsName.USER_TYPE.FREE);
        if (!freeCode.equals(user_1.getType())) {
            throw new ServiceException(User_ErrorsEnum.COMPANY_BASICINFOSERVICE_POST_201);
        }

        /* 验证公司名称 */
        String name = body.getName();
        int existCount = companyDaoHelper.getCountByName(name);
        if (existCount > 0) {
            throw new ServiceException(User_ErrorsEnum.COMPANY_BASICINFOSERVICE_POST_202);
        }

        /* 创建公司 */
        CompanyEntity company_2 = companyCustomConvertor.company_BasicInfoPostModel2entity(body);
        company_2.setCreateTime(new Date());
        company_2.setCreateUserId(userCard.getUserId());
        company_2.setResourceCode("0");
        company_2.setIsAuth(CommonOptions.YES_NO.NO);
        company_2.setIsValid(CommonOptions.YES_NO.YES);
        company_2.setState("1");
        int changeCount = companyDao.insert(company_2);
        /* 更新公司的资源代码 */
        int companyId_2 = company_2.getId();
        CompanyCustomQueryModel companyCustomQueryModel = new CompanyCustomQueryModel();
        companyCustomQueryModel.setEqual_id(companyId_2);
        String companyResourceCode = "01" + HzhrStringUtils.fillZeroForLeft(companyId_2 + "", 4);
        companyCustomQueryModel.setSet_resourceCode(companyResourceCode);
        companyDao.updateProps(companyCustomQueryModel);

        /* 创建同名顶级部门 */
        DeptEntity dept_1 = new DeptEntity();
        dept_1.setName(company_2.getName());
        dept_1.setCode(company_2.getId() + "-01");
        dept_1.setLevel(1);
        dept_1.setParentId(0);
        dept_1.setCompanyId(companyId_2);
        dept_1.setCreateTime(new Date());
        dept_1.setCreateUserId(userCard.getUserId());
        dept_1.setResourceCode(companyResourceCode + "01");
        dept_1.setIsValid(CommonOptions.YES_NO.YES);
        dept_1.setState("1");
        deptDao.insert(dept_1);

        /* 更新登录用户信息 */
        UserCustomQueryModel userCustomQueryModel = new UserCustomQueryModel();
        userCustomQueryModel.setEqual_id(loginUserId);
        userCustomQueryModel.setSet_companyId(company_2.getId());
        userCustomQueryModel.setSet_deptId(dept_1.getId());
        userCustomQueryModel.setSet_userCode(companyResourceCode);
        userCustomQueryModel.setSet_resourceCode(companyResourceCode);
        userCustomQueryModel.setSet_type(userOptions.getCode(UserOptionsTypeName.USER_TYPE, UserOptionsName.USER_TYPE.COMPANY_CREATOR));
        userDao.updateProps(userCustomQueryModel);

        /* 返回信息 */
        Company_BasicInfoGetModel result = company_basicInfoService.get(initHeader(header, AccessModeEnum.GLOBAL), query);
        result.setChangeCount(changeCount);
        return result;
    }

    //region javadoc注释 : get
    /**
     * 用户查询其创建的公司的基本信息 <br/><br/>
     * 简介&nbsp;:&nbsp;查询公司信息 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询公司信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;查询登录人所创建的公司(COMPANY)； <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数和查询到的结果组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @throws ServiceException <br/>
     * @see Company_BasicInfoGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //用户查询其创建的公司的基本信息
    //简介 : 查询公司信息
    //步骤 :
    //1. 查询公司信息 : (√)
    // 查询登录人所创建的公司(COMPANY)；
    //2. 返回信息 : (√)
    // 根据条件参数和查询到的结果组装响应并返回；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/companys/basic-infos", method = RequestMethod.GET)
    //endregion
    public Company_BasicInfoGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询公司信息 */
        //生成查询模型
        UserCardModel userCard = getUserCard(header);
        CompanyCustomQueryModel queryModel = companyCustomConvertor.company_BasicInfoService_query2queryModel(query);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        queryModel.setEqual_createUserId(userCard.getUserId());
        //查询实体
        List<CompanyEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = companyDao.selectExtList(queryModel);
        }

        /* 返回信息 */
        Company_BasicInfoGetModel result = companyCustomConvertor.extList2company_BasicInfoGetModel(extList, null, null, query);
        return result;
    }

    //region javadoc注释 : get1
    /**
     * 用户查询其所属的公司的基本信息 <br/><br/>
     * 简介&nbsp;:&nbsp;查询公司信息 <br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询公司信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;查询登录人所属的公司(COMPANY)； <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * &nbsp;根据条件参数和查询到的结果组装响应并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @throws ServiceException <br/>
     * @see Company_BasicInfoGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get1
    //用户查询其所属的公司的基本信息
    //简介 : 查询公司信息
    //步骤 :
    //1. 查询公司信息 : (√)
    // 查询登录人所属的公司(COMPANY)；
    //2. 返回信息 : (√)
    // 根据条件参数和查询到的结果组装响应并返回；
    //endregion
    //region 参数 : get1
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : get1
    @RequestMapping(value = "/v1/companys/basic-infos", method = RequestMethod.GET)
    //endregion
    public Company_BasicInfoGetModel get1(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询公司信息 */
        //生成查询模型
        UserCardModel userCard = getUserCard(header);
        CompanyCustomQueryModel queryModel = companyCustomConvertor.company_BasicInfoService_query2queryModel(query);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        queryModel.setEqual_id(userCard.getCompanyId());
        //查询实体
        List<CompanyEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = companyDao.selectExtList(queryModel);
        }

        /* 返回查询信息 */
        Company_BasicInfoGetModel result = companyCustomConvertor.extList2company_BasicInfoGetModel(extList, null, null, query);
        return result;
    }
}









































































































